<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 优化后的斐波数列
        let n = 5;
        let cache = {};
        if (n < 2) {
            throw new Error('n 太小了~');
        }

        function fib(n) {
            if (cache.hasOwnProperty(n)) {
                return cache[n]
            }

            let t = null;
            if (n <= 1) {
                t = 1;
            }
            else {
                t = fib(n - 1) + fib(n - 2);
            } 

            console.log('fib...', cache);
            console.count("计数");

            cache[n] = t;
            return t;
        }

        // console.log(fib(n));
        // console.log("=============================");
        // console.log(fib(n));
        // console.log(fib(n));
        // console.log(fib(n));

        for (let i = 0; i < 10; i++) {
            fib(i)
        }
    </script>
</body>

</html>